#include "smallblock.h"
#include "ui_smallblock.h"
#include <QKeyEvent>
#include <QDebug>
#include "voluecontrol.h"
#include <QRegExpValidator>
#include <QRegExp>
#include <QKeyEvent>

SmallBlock::SmallBlock(int type, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SmallBlock),
    m_type(type),
    m_text(""),
    m_volctrl(NULL),
    cursorpos(0)
{
    ui->setupUi(this);
    ui->stackedWidget->setCurrentIndex(type);
    ui->lineEdit->installEventFilter(this);
    ui->lineEdit->setContextMenuPolicy(Qt::NoContextMenu);
    if (type == 2)
    {
        m_volctrl = new VolueControl;
        m_volctrl->setValue(20);
        ui->verticalLayout_4->addWidget(m_volctrl);
    }
}

SmallBlock::~SmallBlock()
{
    delete ui;
}

void SmallBlock::setLineEditPlaceHolder(const QString& txt)
{
    ui->lineEdit->setPlaceholderText(txt);
}

void SmallBlock::setLineEditReadOnly(bool r)
{
    ui->lineEdit->setReadOnly(r);
    ui->lineEdit->setProperty("rd",r);
    ui->lineEdit->style()->unpolish(ui->lineEdit);
    ui->lineEdit->style()->polish(ui->lineEdit);
}

void SmallBlock::setSelectionList(QStringList &list, QString df)
{
    m_selectionMap.clear();
    int i = 0;
    int d = 0;
    foreach(QString s, list)
    {
        if (s == df)
        {
            d = i;
        }
        m_selectionMap.insert(i,s);
        i += 1;
    }
    m_current = df.isEmpty() ? 0 : d;
    if (m_selectionMap.count() > 0)
    {
        ui->lbText->setText(m_selectionMap.value(m_current));
    }
}

bool SmallBlock::eventFilter(QObject *o, QEvent *e)
{
    if (o == ui->lineEdit)
    {
        if (e->type() == QEvent::FocusOut)
        {
            if (m_type == 1)
            {
                ui->label_4->setProperty("selffocus","0");
                ui->label_5->setProperty("selffocus","0");
                ui->label_4->style()->unpolish(ui->label_4);
                ui->label_4->style()->polish(ui->label_4);

                ui->label_5->style()->unpolish(ui->label_5);
                ui->label_5->style()->polish(ui->label_5);
            } else if (m_type == 0)
            {
                if ((m_text != ui->lineEdit->text()))
                {
                    m_text = ui->lineEdit->text();
                    emit textFinishedSig(m_text);
                }
                ui->lineEdit->setCursorPosition(ui->lineEdit->text().count());
            }
            ui->frame->setProperty("selffocus","0");
            ui->frame->style()->unpolish(ui->frame);
            ui->frame->style()->polish(ui->frame);
            update();
        } else if(e->type() == QEvent::KeyRelease)
        {

            QKeyEvent *ke = dynamic_cast<QKeyEvent *>(e);
            if (ke && (ke->key() == Qt::Key_Right))
            {
                cursorpos++;
            }
            else if (ke && (ke->key() == Qt::Key_Left))
            {
                cursorpos--;
            }
            else if (ke && (ke->key() == Qt::Key_Backspace))
            {
                if(cursorpos > 0)
                {
                    cursorpos--;
                }
            }
            else if (ke && ((ke->key() >= Qt::Key_0
                            && ke->key() <= Qt::Key_9)
                            ||(ke->key() == Qt::Key_Period) ))
            {
                cursorpos++;
            }
        }

    }
    return false;
}

void SmallBlock::focusInEvent(QFocusEvent *e)
{
    if (m_type == 1)
    {
        ui->label_4->setProperty("selffocus","1");
        ui->label_5->setProperty("selffocus","1");

        ui->label_4->style()->unpolish(ui->label_4);
        ui->label_4->style()->polish(ui->label_4);

        ui->label_5->style()->unpolish(ui->label_5);
        ui->label_5->style()->polish(ui->label_5);
    }
    ui->frame->setProperty("selffocus","1");
    ui->frame->style()->unpolish(ui->frame);
    ui->frame->style()->polish(ui->frame);

    QWidget::focusInEvent(e);
}

void SmallBlock::focusOutEvent(QFocusEvent *e)
{
    if (ui->lineEdit->hasFocus())
    {
        QWidget::focusOutEvent(e);
        return;
    }

    if (m_type == 1)
    {
        ui->label_4->setProperty("selffocus","0");
        ui->label_5->setProperty("selffocus","0");
        ui->label_4->style()->unpolish(ui->label_4);
        ui->label_4->style()->polish(ui->label_4);

        ui->label_5->style()->unpolish(ui->label_5);
        ui->label_5->style()->polish(ui->label_5);
    }

    ui->frame->setProperty("selffocus","0");
    ui->frame->style()->unpolish(ui->frame);
    ui->frame->style()->polish(ui->frame);
    QWidget::focusOutEvent(e);
}

void SmallBlock::setLineEditValidator(QString regstr)
{
    QRegExp regExp(regstr);
    ui->lineEdit->setValidator(new QRegExpValidator(regExp,this));
}

void SmallBlock::keyReleaseEvent(QKeyEvent *e)
{
    if (e->key() == Qt::Key_Enter)      //给lineedit使用，主要是完成输入后，keyboard会发送一个enter事件
    {
        if (m_type == 0)
        {
            if ((m_text != ui->lineEdit->text()))
            {
                m_text = ui->lineEdit->text();
                emit textFinishedSig(m_text);
            }
            emit receiveEnter();
            this->setFocus();
        }
    }else if (e->key() == Qt::Key_Left) //音量键 减
    {
        if (m_type == 2)
        {
            int val = m_volctrl->getValue();
            if (val < 2)
            {
                val = 0;
            }
            else
            {
                val = val-2;
            }
            if (val != m_volctrl->getValue())
            {
                emit volChangedSig(val);
            }
        }
    } else if (e->key() == Qt::Key_Right) //音量键 加
    {
        if (m_type == 2)
        {
            int val = m_volctrl->getValue();
            if (val >= 18)
            {
                val = 20;
            }
            else
            {
                val = val+2;
            }
            if (val != m_volctrl->getValue())
            {
                emit volChangedSig(val);
            }
        }
    } else
    {

    }
    QWidget::keyReleaseEvent(e);
}
int SmallBlock::mtype() const
{
    return m_type;
}

void SmallBlock::setmType(int type)
{
    m_type = type;
}

int SmallBlock::getCursorPos()
{
    return cursorpos;
}

void SmallBlock::mouseReleaseEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton)      //切换控件使用
    {
        if (m_type == 1)
        {
            int index = getNextIndex();
            emit optionChangeSig(index);
        }
        else if (m_type == 0)
        {
            ui->lineEdit->setFocus();
            cursorpos = ui->lineEdit->cursorPosition();
            emit textStartSig();
        }
    }
    QWidget::mouseReleaseEvent(e);
}

void SmallBlock::setBlockEnable(bool s)
{
    this->setEnabled(s);
    ui->frame->setProperty("selfenable",s);
    setLineEditReadOnly(!s);
//    if (m_type == 1)
//    {
//        if (s == false)
//        {
//            ui->lbText->setText("");
//        }
//        else
//        {
//            ui->lbText->setText(m_selectionMap.value(m_current));
//            emit selctionChanged(m_selectionMap.value(m_current));
//        }
//    }

    ui->frame->style()->unpolish(ui->frame);
    ui->frame->style()->polish(ui->frame);
}

void SmallBlock::setText(const QString &txt)
{
    ui->lineEdit->setText(txt);
    m_text = txt;
}

QString SmallBlock::getText()
{
    return ui->lineEdit->text();
}

void SmallBlock::setTitleLabel(QString txt)
{
    ui->title_label->setText(txt);
}

void SmallBlock::setVol(int vol)
{
    if (m_type == 2)
    {
        m_vol = vol;
        m_volctrl->setValue(vol);
    }
}

int SmallBlock::getVol()
{
    return m_volctrl->getValue();
}

void SmallBlock::changeOption()
{
    if (m_type == 1)
    {
        m_current = (m_current+1) % m_selectionMap.keys().count();
        ui->lbText->setText(m_selectionMap.value(m_current));
        emit selctionChanged(m_selectionMap.value(m_current));
    }
}

int SmallBlock::getNextIndex()
{
    return (m_current+1) % m_selectionMap.keys().count();
}

void SmallBlock::setOption(int index)
{
//    if (index == m_current)
//    {
//        return;
//    }
    m_current = index;
    if (m_selectionMap.count() > 0)
    {
        ui->lbText->setText(m_selectionMap.value(m_current));
        emit selctionChanged(m_selectionMap.value(m_current));
    }
}

QLineEdit * SmallBlock::lineEdit()
{
    return ui->lineEdit;
}

